﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace GraphicLib
{
    public static class ProductCheck
    {
        /// <summary>
        /// 默认切割次数
        /// </summary>
        public static int SPLIT_COUNT=6;

        public static readonly int SPLIT_MAX_COUNT = 12;
        /// <summary>
        /// 产品方差容错值
        /// </summary>
        /// <param name="VARIANCE_TOLERANCE"></param>
        public static double VARIANCE_TOLERANCE = 0.9;

        public static void CheckProducts(TextBox txtLog)
        {
            string goodProductFilePath = Sample.GOOD_PRODUCT_DIR;
            string badProductFilePath = Sample.BAD_PRODUCT_DIR;
            
            //获取切割次数
            if ((SPLIT_COUNT == 0) || (SPLIT_COUNT > SPLIT_MAX_COUNT))
            {
                SPLIT_COUNT = SPLIT_MAX_COUNT;
            }
            
            //先得到产品图像
            Dictionary<string, Bitmap> bitmaps = BitmapGetter.GetProductBitmaps();

            //得到产品图像灰度化数组
            foreach (string bitmapKey in bitmaps.Keys)
            {
                StringBuilder log = new StringBuilder();
                //输出切割次数
                log.AppendFormat("图像切割次数：{0}\r\n", SPLIT_COUNT);
                //输出当前图像处理文件名
                log.AppendFormat("{1} 当前处理图像：{0}\r\n", bitmapKey, DateTime.Now);
                byte[] grayBytes = GrayConvertor.GetGrayBytes(bitmaps[bitmapKey]);
                log.AppendFormat("图像灰度化处理完成.{0}\r\n", DateTime.Now);
                //切割灰度化数组
                List<byte[]> bytesList = GraphicSplit.SingleSplitGraphicBytes(grayBytes, SPLIT_COUNT);
                log.AppendFormat("图像切割处理完成.{0}\r\n", DateTime.Now);

                bool goodProduct = true;
                string singleCheckResult="";
                string fileName=bitmapKey.Substring(bitmapKey.LastIndexOf(@"\")+1);

                List<double> doubleSampleList = Sample.variencesList[SPLIT_COUNT-1];
                for (int j = 0; j < bytesList.Count; j++)
                {
                    //比较产品图像与样本方差
                    double newVariance = VarianceCalculator.GetProductVariance(bytesList[j]);                        
                    goodProduct = VarianceComparer.CompareVariance(doubleSampleList[j], newVariance);
                    //如果有一个false，则认定为坏品,并将产品图像分类
                    if (!goodProduct)
                    {
                        //将坏品输出到坏品图像文件夹，在此处进行串口通信编程                       
                        singleCheckResult = string.Format("检测结果：第{1}次分割时检测出坏品\r\n图片转移到：{0}\r\n", badProductFilePath + "\\" + Guid.NewGuid().ToString() + FileUtils.GetExtension(bitmapKey), SPLIT_COUNT);
                        File.Copy(bitmapKey, badProductFilePath + "\\" + Guid.NewGuid().ToString() + FileUtils.GetExtension(bitmapKey));
                        break;
                    }
                }
                if (goodProduct)
                {
                    singleCheckResult = string.Format("检测结果：好品\r\n图片转移到：{0}\r\n", goodProductFilePath + "\\" + Guid.NewGuid().ToString() + FileUtils.GetExtension(bitmapKey));
                    File.Copy(bitmapKey, goodProductFilePath + "\\" + Guid.NewGuid().ToString() + FileUtils.GetExtension(bitmapKey));
                }
                bitmaps[bitmapKey].Dispose();              
                try
                {
                    File.Delete(bitmapKey);
                }
                catch (Exception ex)
                {
                    log.Append(ex.Message);
                }
                log.Append(singleCheckResult);
                log.AppendFormat("完成时间：{0}\r\n", DateTime.Now);
                //txtLog.AppendText(log.ToString());
                ProcessThread.SetMyTextBoxValue(log.ToString(), txtLog);
            }
            //return log;
        }
    }
}
